//
//  LAMerchantDetailReviewTableView.m
//  LYLazizDevelopDemo
//
//  Created by xbn_iMac on 2016/10/24.
//  Copyright © 2016年 XBN. All rights reserved.
//

#import "LAMerchantDetailReviewTableView.h"
#import "UITableView+FDTemplateLayoutCell.h"
#import "LAReviewTableViewCell.h"
#import "LAReviewModel.h"
#import "LAReviewHeaderView.h"
#import "UITableView+LZTableView.h"
#import "LZNoDataPublicView.h"

@interface LAMerchantDetailReviewTableView ()<UITableViewDelegate,UITableViewDataSource,LAReviewCellDelegate,ReviewHeaderClickDelegate>

@property (nonatomic, strong) NSMutableArray *dataArr;
@property (nonatomic, strong) UIControl *selectedButton;
@property (nonatomic, assign) NSInteger selectedHeaderButtonTag;

@property (nonatomic, strong) LAReviewHeaderView *headerView;

@property (nonatomic, assign) int page;
@property (nonatomic, assign) int limit;

@property (nonatomic, assign) NSTimeInterval request_timeStemp;

@end

@implementation LAMerchantDetailReviewTableView

-(instancetype)initWithFrame:(CGRect)frame style:(UITableViewStyle)style {
    self = [super initWithFrame:frame style:style];
    if (self) {
        self.delegate = self;
        self.dataSource = self;
        self.separatorStyle = UITableViewCellSeparatorStyleNone;
        self.separatorColor = [UIColor clearColor];
        [self registerNib:[UINib nibWithNibName:@"LAReviewTableViewCell" bundle:nil] forCellReuseIdentifier:@"cell"];
        [self configBaseData];
        
        self.estimatedRowHeight = 44.0;
        self.rowHeight = UITableViewAutomaticDimension;
    }
    return self;
}

- (void)checkHaveData {
    if (self.dataArr.count == 0) {
        if (self.subTableDelegate && [self.subTableDelegate respondsToSelector:@selector(requestDateWithType:andPrameter:andPage:andLimit:resultSuccess:)]) {
            if (self.mj_footer.state != MJRefreshStateRefreshing) {
                [self.mj_footer beginRefreshing];
            }
        }
    }
    if (self.tableHeaderView == nil) {
        [self createHeaderView];
    }
}

- (void)upData {
    [self clickButton:self.selectedButton];
    
    if (self.reviewTableDelegate && [self.reviewTableDelegate respondsToSelector:@selector(requestCommentCountResultSuccess:)]) {
        [self.reviewTableDelegate requestCommentCountResultSuccess:^(id result) {
            self.headerView.model = result;
        }];
    }
}

/* --------------------- configure Super Refresh method --------------------- */

- (void)upLoadMoreData {
    [self requestListWithType:self.selectedHeaderButtonTag andPage:self.page];
}

- (void)requestListWithType:(NSInteger)type andPage:(int)aPage {
    
    WS(weakSelf)
    
    [self.subTableDelegate requestDateWithType:TableViewType_Review andPrameter:@{@"type":[NSString stringWithFormat:@"%ld",(long)type],@"timestamp":[NSNumber numberWithLong:self.request_timeStemp]} andPage:aPage andLimit:self.limit resultSuccess:^(NSArray *result,NSNumber *timeStemp) {
        
        [weakSelf.mj_footer endRefreshing];

        if (result && type == weakSelf.selectedHeaderButtonTag && aPage == weakSelf.page) {
            if (result) {
                
                weakSelf.page ++;
                if (weakSelf.request_timeStemp == 0) {
                    weakSelf.request_timeStemp = [timeStemp longLongValue];
                }
                
                if (result.count>0) {
                    if (weakSelf.isShowNoDataImageView) {
                        [LZNoDataPublicView hiddenNoDataViewInView:weakSelf];
                    }
                    [weakSelf.dataArr addObjectsFromArray:result];
                    [weakSelf reloadData];
                }
                else {
                    if (weakSelf.dataArr.count == 0) {
                        if (self.isShowNoDataImageView) {
                            [LZNoDataPublicView showNoDataViewInView:weakSelf];
                            [weakSelf setNullStringFooterText];
                        }
                        else {
                            [weakSelf setNoDataFooterText];
                        }
                    }
                    else {
                        [weakSelf setNoMoreDataFooterText];
                    }
                    [weakSelf.mj_footer endRefreshingWithNoMoreData];
                }
            }
        }
        
        
    }];
}

#pragma mark - Private Method

- (void)configBaseData {
    self.page = 1;
    self.limit = 10;
    self.dataArr = [NSMutableArray array];
    self.request_timeStemp = 0;
}

-(void)createHeaderView {
    LAReviewHeaderView *header = [[LAReviewHeaderView alloc] init];
    header.frame = CGRectMake(0, 0, [header intrinsicContentSize].width, [header intrinsicContentSize].height);
    header.delegate = self;
    self.tableHeaderView = header;
    self.headerView = header;
    
    if (self.reviewTableDelegate && [self.reviewTableDelegate respondsToSelector:@selector(requestCommentCountResultSuccess:)]) {
        [self.reviewTableDelegate requestCommentCountResultSuccess:^(id result) {
            header.model = result;
        }];
    }
}

#pragma mark - ReviewHeaderClickDelegate

- (void)clickButton:(UIControl *)button {
    self.selectedButton = button;
    self.selectedHeaderButtonTag = button.tag;
    [self.mj_footer endRefreshing];
    [self.dataArr removeAllObjects];
    [self reloadData];
    
    self.page = 1;
    self.limit = 10;
    self.request_timeStemp = 0;
    
    [self checkHaveData];
}

#pragma mark - UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.dataArr.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    LAReviewTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    [self configureCell:cell atIndexPath:indexPath];
    
    return cell;
}

- (void)configureCell:(LAReviewTableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath {
    cell.fd_enforceFrameLayout = NO; // Enable to use "-sizeThatFits:"
    
    cell.indexPath = indexPath;
    cell.delegate = self;
    cell.reviewType = self.reviewType;
    cell.headImageType = HeadImageType_User;
    
    cell.entity = self.dataArr[indexPath.section];
}

#pragma mark - UITableViewDelegate

//- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
//    LAReviewModel *model = self.dataArr[indexPath.section];
//
//    float height = [tableView fd_heightForCellWithIdentifier:@"cell" cacheByIndexPath:indexPath configuration:^(LAReviewTableViewCell *cell) {
//    
//        [cell getReviewLabel].numberOfLines = model.labReviewNumberOfLine;
//        
//        [self configureCell:cell atIndexPath:indexPath];
//    }];
//    
//    return height;
//}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if (section == 0) {
        return 10;
    }
    return 0.01;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0.01;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

-(void)refreshCellHeight:(NSIndexPath *)indexPath {
    [self reloadData];
}


@end
